<!doctype html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security" xmlns:auth=""
      class="x-admin-sm">

<auth:data uri="/system/user/list.json,/system/dept/list.json,/system/job/list.json"/>

<head>
    <th:block th:include="common/head::noXadmin('用户管理')"/>
    <script type="application/javascript" th:src="@{/static/js/jquery.min.js}"></script>
    <script type="application/javascript" th:src="@{/static/lib/ztree/js/jquery.ztree.all.min.js}"></script>
    <link rel="stylesheet" type="text/css" th:href="@{/static/lib/ztree/css/metroStyle/metroStyle.css}"/>
</head>
<body>
<div class="layui-row " style="height:100%;position: absolute;left: 0px;right: 0px;top: 0px;bottom: 0px;">
    <div class="layui-col-md1" style="overflow: scroll;height: 100%;width: 220px;" id="deptBar">
        <div class="layui-card " style="height: 100%;">
            <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
                <ul class="layui-tab-title">
                    <li class="layui-this">组织架构</li>
                    <li>岗位</li>
                </ul>
                <div class="layui-tab-content">
                    <div class="layui-tab-item layui-show">
                        <ul id="deptBox" class="ztree"></ul>
                    </div>
                    <div class="layui-tab-item">
                        <ul id="jobBox" class="fish-list">
                            <li job-id="0" class="selected">全部</li>
                            <th:block th:each="j : ${jobs}">
                                <li th:attr='job-id=${j.jobId}' th:text="${j.jobName}"></li>
                            </th:block>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        <div class="split-line-x split-line-x-open">
            <i class="fa fa-caret-left"></i>
        </div>
    </div>
    <div id="mainBox" style="height:100%;position: absolute;left: 220px;top:0px;right: 0px;">
        <div class="split-line-x split-line-x-closed">
            <i class="fa fa-caret-right"></i>
        </div>
        <div class="layui-card " id="queryBox">
            <div class="layui-card-body ">
                <form class="layui-form layui-col-space5" lay-filter="searchForm">
                    <input type="hidden" id="deptId" name="deptId" th:value="${deptId==null?1:deptId}"/>
                    <input type="hidden" id="jobId" name="jobId" th:value="${jobId==null?0:jobId}"/>
                    <div class="layui-inline layui-show-xs-block">
                        <input class="layui-input" autocomplete="off" placeholder="开始日" name="stime" id="start">
                    </div>
                    <div class="layui-inline layui-show-xs-block">
                        <input class="layui-input" autocomplete="off" placeholder="截止日" name="etime" id="end"></div>
                    <div class="layui-inline layui-show-xs-block">
                        <input type="text" name="username" placeholder="请输入用户名" autocomplete="off"
                               class="layui-input"></div>
                    <div class="layui-inline layui-show-xs-block">
                        <input type="text" name="name" placeholder="请输入姓名" autocomplete="off"
                               class="layui-input"></div>
                    <div class="layui-inline layui-show-xs-block">
                        <select name="status" lay-verify="">
                            <option value="">状态</option>
                            <option value="0"
                                    th:each="status:${T(com.hardydou.common.constant.Const.UserStatus).values()}"
                                    th:value="${status.val}"
                                    th:text="${status.name}">
                            </option>
                        </select>
                    </div>
                    <div class="layui-inline layui-show-xs-block">
                        <button class="layui-btn" lay-submit="" lay-filter="sreach">
                            <i class="layui-icon">&#xe615;</i></button>
                    </div>
                </form>
            </div>

        </div>
        <div class="split-line-y split-line-y-open">
            <i class="fa "></i>
        </div>
        <div class="layui-card ">
            <div class="layui-card-body ">
                <table class="layui-table" id="dataList" lay-filter="data"></table>
            </div>
        </div>
    </div>
</div>
</body>
<th:block th:inline="none">
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-group">
            <button type="button" class="layui-btn layui-btn-sm" lay-event="add">
                <i class="layui-icon layui-icon-add-1"></i>
            </button>
            <button type="button" class="layui-btn layui-btn-sm" lay-event="moveDept">
                <i class="layui-icon layui-icon-transfer"></i>
            </button>
            <button type="button" class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delAll">
                <i class="layui-icon layui-icon-delete"></i>
            </button>
        </div>
    </script>
    <script type="text/html" id="sexTml">
        <!-- 这里的checked的状态只是演示 -->
        {{d.sex=="N"?"未知":(d.sex=="F"?"男":"女")}}
    </script>
    <script type="text/html" id="statusTpl">
        <!-- 这里的checked的状态只是演示 -->
        <input type="checkbox" name="status" value="{{d.userId}}" lay-skin="switch" lay-text="启用|停用"
               {{d.status==0?"checked":""}} lay-filter="status" disabled>
    </script>
    <script type="text/html" id="barTpl">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail"
           sec:authorize="hasAuthority('system:user:info')">查看</a>
        <a class="layui-btn layui-btn-warm layui-btn-xs " lay-event="on-off"
           sec:authorize="hasAuthority('system:user:status')">{{d.status==0?"停用":"启用"}}</a>
        <a class="layui-btn layui-btn-xs" lay-event="edit" sec:authorize="hasAuthority('system:user:edit')">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="resetPassword"
           sec:authorize="hasAuthority('system:user:password')">重置密码</a>
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="auth"
           sec:authorize="hasAuthority('system:user:auth')">授权角色</a>
    </script>
    <script type="application/javascript">
        $(".split-line-x-open >i ").click(function (obj) {
            $("#deptBar").hide();
            $(".split-line-x-closed").show();
            $("#mainBox").css('left', 0);
        })
        $(".split-line-x-closed >i ").click(function () {
            $("#deptBar").show();
            $(".split-line-x-closed").hide();
            $("#mainBox").css('left', 220);
        });
        var queryBoxShow = true;
        $(".split-line-y >i").click(function () {
            if (queryBoxShow) {
                queryBoxShow = false;
                $("#queryBox").slideUp('fast');
                $(".split-line-y").removeClass('split-line-y-open').addClass('split-line-y-closed');
            } else {
                queryBoxShow = true;
                $("#queryBox").slideDown('fast');
                $(".split-line-y").removeClass('split-line-y-closed').addClass('split-line-y-open');
            }

        });
        var deptTreeSetting = {
                async: {
                    enable: true,
                    type: "get",
                    url: "/system/dept/list.json",
                    dataFilter: function (treeId, parentNode, responseData) {
                        var data = responseData.data.list;
                        for (var i = 0; i < data.length; i++) {
                            data[i].icon = "";
                            if ($("#deptId").val() == data[i].deptId) {
                                data[i].open = true;
                            }
                        }
                        return data;
                    }
                },
                check: {
                    enable: false,
                    chkStyle: "checkbox"
                },
                data: {
                    key: {
                        name: "deptName",
                        title: "deptName"
                    },
                    simpleData: {
                        enable: true,
                        idKey: "deptId",
                        pIdKey: "parentId",
                        rootPId: 0
                    }
                }, callback: {
                    onClick: function (event, treeId, treeNode) {
                        $("#deptId").val(treeNode.deptId);
                        layui.use(['table', 'form'], function () {
                            var table = layui.table
                                , form = layui.form;
                            var data = form.val("searchForm");
                            table.reload('dataList', {
                                    page: {
                                        curr: 1
                                    },
                                    where: data
                                }
                            )
                        })
                    },
                    onAsyncSuccess: function (event, treeId, treeNode) {
                        zTreeObj.expandAll(true);
                        var s = zTreeObj.getNodeByParam("deptId", $("#deptId").val());
                        zTreeObj.selectNode(s);
                    }
                }
            },
            zTreeObj = $.fn.zTree.init($("#deptBox"), deptTreeSetting)
        ;
    </script>
    <script>
        layui.use(['table', 'form'],
            function () {
                var table = layui.table,
                    form = layui.form;
                table.render({
                    elem: '#dataList'
                    , toolbar: '#toolbarDemo'
                    , totalRow: false
                    , url: '/system/user/list.json' //数据接口
                    , where: {stime: '', etime: '', username: '', name: '', orderBy: 'ctime desc'}
                    , page: true //开启分页
                    , response: {
                        statusCode: 0
                    }
                    , cols: [[
                        {checkbox: true, type: 'checkbox', fixed: 'left'}
                        , {field: 'userId', title: 'ID', width: 80, sort: true, fixed: 'left'}
                        , {field: 'username', title: '用户名', width: 120,}
                        , {field: 'name', title: '姓名', width: 100,}
                        , {field: 'sex', title: '性别', templet: '#sexTml', width: 80}
                        , {field: 'mobile', title: '手机号', width: 120}
                        , {field: 'email', title: '邮箱', minWidth: 150}
                        // , {field: 'avatar', title: '头像', width: 80}
                        , {field: 'status', title: '状态', templet: '#statusTpl', width: 100}
                        , {field: 'ctime', title: '创建时间', width: 180}
                        , {title: '操作', align: 'center', templet: '#barTpl', minWidth: 350}
                    ]],
                    parseData: function (res) {
                        return {
                            code: res.code,
                            msg: res.message,
                            count: res.data.total,
                            data: res.data.list,
                            totalRow: {
                                score: "score",
                                experience: "exp"
                            }
                        }

                    }
                    , request: {
                        pageName: 'pageNum' //页码的参数名称，默认：page
                        , limitName: 'pageSize' //每页数据量的参数名，默认：limit
                    }
                })
                //监听工具条
                table.on('tool(data)',
                    function (obj) {
                        var data = obj.data
                            , event = obj.event //事件
                        ;
                        switch (event) {
                            case 'on-off':
                                $.ajax({
                                    url: rootPath + 'system/user/status?userId=' + data.userId,
                                    type: "PUT",
                                    dataType: "json",
                                    success: function (data) {
                                        layer.msg(data.message);
                                    },
                                    error: function (xhr, status) {
                                        layer.msg(xhr.responseJSON.message);
                                    }
                                });
                                break;
                            case 'edit':
                                xadmin.open('修改账户', rootPath + 'system/user/edit?userId=' + data.userId)
                                break;
                            case 'detail':
                                xadmin.open('用户详情', rootPath + 'system/user/info?userId=' + data.userId)
                                break;
                            case 'auth':
                                xadmin.open('分配角色', rootPath + 'system/user/auth?userId=' + data.userId)
                                break;
                            case 'resetPassword':
                                layer.prompt({
                                    title: '重置密码(' + data.username + ')',
                                    formType: 1
                                }, function (pass, index) {
                                    $.ajax({
                                        url: rootPath + 'system/user/password.json',
                                        data: {
                                            userId: data.userId,
                                            password: pass
                                        },
                                        type: "PUT",
                                        dataType: "json",
                                        success: function (data) {
                                            console.log(data);
                                            layer.msg(data.message);
                                            layer.close(index);
                                        },
                                        error: function (data) {
                                            layer.msg(data.message);
                                        }
                                    });

                                });
                                break;
                            case 'del':
                                layer.msg('你确定删除么？', {
                                    time: 2000,//2秒自动关闭
                                    btn: ['确定', '取消'],
                                    yes: function (index) {
                                        $.ajax({
                                            url: rootPath + 'system/user/del?userIds=' + data.userId,
                                            type: "DELETE",
                                            dataType: "json",
                                            success: function (data) {
                                                layer.msg(data.message);
                                                location.reload(); //删除成功后再刷新
                                            },
                                            error: function (data) {
                                                layer.msg(data.message);
                                            }
                                        });
                                        layer.close(index);
                                    }
                                })
                                break;

                        }
                    });

                //头工具栏事件
                table.on('toolbar(data)',
                    function (obj) {
                        let checkedData = table.checkStatus('dataList').data;
                        var ids = "";
                        checkedData.forEach(function (item, idx) {
                            ids += item.userId + ",";
                        });
                        ids = ids.substr(0, ids.length - 1);
                        switch (obj.event) {
                            case 'add':
                                xadmin.open('添加用户', rootPath + 'system/user/add' + "?deptId="
                                    + $("#deptId").val())
                                break;
                            case 'delAll':
                                if (ids == "") {
                                    layer.alert("请先选择员工！")
                                    return;
                                }
                                layer.msg('你确定删除么？', {
                                    time: 2000,//2秒自动关闭
                                    btn: ['确定', '取消'],
                                    yes: function (index) {
                                        $.ajax({
                                            url: rootPath + 'system/user/del?userIds=' + ids,
                                            type: "DELETE",
                                            dataType: "json",
                                            success: function (data) {
                                                layer.msg(data.message);
                                                location.reload(); //删除成功后再刷新
                                            },
                                            error: function (data) {
                                                layer.msg(data.message);
                                            }
                                        });
                                        layer.close(index);
                                    }
                                })
                                break;
                            case 'moveDept':
                                if (ids == "") {
                                    layer.alert("请先选择员工！")
                                    return;
                                }
                                layer.msg(ids)
                                ;
                                break;
                        }
                        ;
                    })
                ;
                form.on('submit(sreach)', function (data) {
                    table.reload('dataList', {
                            page: {
                                curr: 1
                            },
                            where: data.field
                        }
                    )
                    return false;
                })
            })
        ;

        $(".fish-list li").on('click', function (e) {
            $(".fish-list li").removeClass('selected');
            $(this).addClass('selected');
            $("#jobId").val($(this).attr('job-id'));
            layui.use(['table', 'form'], function () {
                var table = layui.table
                    , form = layui.form;
                var data = form.val("searchForm");
                table.reload('dataList', {
                        page: {
                            curr: 1
                        },
                        where: data
                    }
                )
            })
        })
    </script>
    <script>layui.use('laydate',
        function () {
            var laydate = layui.laydate;

            //执行一个laydate实例
            laydate.render({
                elem: '#start' //指定元素
            });

            //执行一个laydate实例
            laydate.render({
                elem: '#end' //指定元素
            });

        });
    </script>
</th:block>
</html>